#include "mainwindow.h"
#include "titlebar.h"
#include <QApplication>
#include <QMainWindow>
#include <QtDBus/QDBusConnection>
#include <QtDBus/QDBusInterface>
#include <QTranslator>
#include <QCoreApplication>
#include <QPixmap>
#include <QDebug>
#include <QMessageBox>
#include <QLibraryInfo>
#include <unistd.h>
#include <errno.h>
#include <sys/file.h>
#include <sys/file.h>
#include <fcntl.h>
#include <syslog.h>
#include <KWindowEffects>
#include "customstyle.h"
#include "util/xatom-helper.h"
#include <log.hpp>
#include <kylin_system/window_management.hpp>
#include <single_application/single_application.hpp>
#include <log.hpp>

#define TRANS_PATH  "/usr/share/kylin-camera/translations/"

int main(int argc, char *argv[])
{
    /* 使用sdk管理日志 */
    qInstallMessageHandler(kdk::kabase::Log::logOutput);
    QString kylinCameraFileName;
    if (argc > 1) {
        kylinCameraFileName = argv[1];
    }
    qDebug() << "Load translation file："<< "kylin-camera_" ;
    printf("Program start ...\n");
    // 适配4K屏
    #if (QT_VERSION >= QT_VERSION_CHECK(5, 12, 0))
        QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
        QApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
    #endif

    #if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
        QApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::PassThrough);
    #endif

    kdk::kabase::QtSingleApplication app(argc, argv);
    app.setWindowIcon(QIcon::fromTheme("kylin-camera"));
    app.setApplicationVersion(APP_VERSION);
    if (app.isRunning()) {
        qDebug() << "is running";
        app.sendMessage(kylinCameraFileName);
        return 0;
    }

    //加载翻译文件
    QString trans_path;
    if (QDir(TRANS_PATH).exists()) {
        trans_path = TRANS_PATH;
    }
    else {
        trans_path = qApp->applicationDirPath() + "/translations";
    }
    QTranslator app_trans;
    QTranslator qt_trans;
    QTranslator trans_SDK;
    QString locale = QLocale::system().name();
    QString qt_trans_path;
    qt_trans_path = QLibraryInfo::location(QLibraryInfo::TranslationsPath);// /usr/share/qt5/translations

    if(!app_trans.load("kylin-camera_" + locale + ".qm", trans_path))
        qDebug() << "Load translation file："<< "kylin-camera_" + locale + ".qm from" << trans_path << "failed!";
    else
        app.installTranslator(&app_trans);

    if(!qt_trans.load("qt_" + locale + ".qm", qt_trans_path))
        qDebug() << "Load translation file："<< "qt_" + locale + ".qm from" << qt_trans_path << "failed!";
    else
        app.installTranslator(&qt_trans);




    QString translatorFileName = QLatin1String("qt_");
    translatorFileName += QLocale::system().name();
    QTranslator *translator = new QTranslator();
    if (translator->load(translatorFileName, QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
    {
        app.installTranslator(translator);
    }
    else
    {
        qDebug() << "加载中文失败";
    }
    if(locale == "zh_CN"){
        if(trans_SDK.load(":/translations/gui_zh_CN.qm")){
            app.installTranslator(&trans_SDK);
        }
    }
    if(locale == "bo_CN"){
        if(trans_SDK.load(":/translations/gui_bo_CN.qm")){
            app.installTranslator(&trans_SDK);
        }
    }

    QStringList strList;
    for(int i = 0; argv[i];i++){
        if(argv[i]){
            QString a = argv[i];
            strList.push_back(a);
            qDebug() << i + ":" + a;
        }
    }
    MainWindow w(strList);
#ifndef __V10__
    //添加窗管协议
    kdk::kabase::WindowManagement::setWindowMotifHint(w.winId());
#endif
    app.setActivationWindow(&w);
    w.show();
    KWindowEffects::enableBlurBehind(w.winId(),true);
    return app.exec();

}
